interface Player {
  bankerCount: number,
  lftId: number,
  nickname: string,
  owner: boolean,
  point: number,
  seatIndex: number,
  suitDescribe: Array<{name: string, count: number}>,
  suitMap: any,
  suitPatterns: any,
  uid: string,
  winner: boolean
}

declare var window: any

class UserLabel extends eui.Component {
  private userGroup: eui.Group
  private useridGroup: eui.Group
  private userContent: eui.Group
  private userScore: eui.Group
  private username: eui.Label
  private userid: eui.Label
  private highest: eui.Label
  private boom: eui.Label
  private winlose: eui.Label
  private zhanjiText: eui.Image
  public player: Player
  constructor(player) {
    super()
    this.once(eui.UIEvent.COMPLETE, this.onComplete, this)
    this.player = player
    this.skinName = `<e:Skin class="JinhuaSkin" width="720" height="244" xmlns:e="http://ns.egret.com/eui" xmlns:w="http://ns.egret.com/wing">
      <e:Group verticalCenter="0" horizontalCenter="0" id="userGroup" width="720" height="244">
        <e:Label id="username" text="" textColor="0xffffff" size="32" x="22" y="72" height="43" width="185"></e:Label>
        <e:Group id="useridGroup" x="22" y="124">
          <e:Label id="userid" text="" size="30" height="43"></e:Label>
        </e:Group>
        <e:Group id="userContent" x="231" y="72" width="261" height="172">
          <e:Label id="highest"></e:Label>        
          <e:Label id="boom"></e:Label>        
          <e:Label id="winlose"></e:Label>        
        </e:Group>
        <e:Image id="zhanjiText" y="72" right="88"></e:Image>
        <e:Group id="userScore" right="0" y="126" width="220" height="68">
        </e:Group>
      </e:Group>
    </e:Skin>`
  }

  scoreNumber(point) {
    const pointSigned = point > 0 ? '+' + point : point
    const numbers = (pointSigned + '').split('')
    return numbers.map(num => {
      if (point >= 0) {
        if (num === '+') {
          return 'pdk-jia-active_png'
        }
        return `pdk-1${num}_png`
      } else {
        if (num === '-') {
          return 'pdk-jian-default_png'
        }
        return `pdk-${num}_png`
      }
    })
  }
  private onComplete(e: eui.UIEvent) {
    const player = this.player

    var hLayout:eui.HorizontalLayout = new eui.HorizontalLayout()
    hLayout.gap = 10
    hLayout.horizontalAlign = egret.HorizontalAlign.CENTER
    this.useridGroup.layout = hLayout   /// 水平布局

    var vLayout:eui.VerticalLayout = new eui.VerticalLayout()
    vLayout.gap = 15
    this.userContent.layout = vLayout //设置问垂直布局

    this.highest.text = `单局最高分: ${player.suitMap.maxPoint}`
    this.boom.text = `打出炸弹数: ${player.suitMap.zhaDanCount}`
    this.winlose.text = `胜负局数: ${player.suitMap.winCount}赢 ${player.suitMap.loseCount}输`

    this.username.text = this.player.nickname.substring(0, 6)
    this.userid.text = 'ID: ' + this.player.lftId
    
    if (this.player.winner) {
      var winnerBg = new eui.Image('faguang_png')
      winnerBg.x = 0
      winnerBg.y = 0
      winnerBg.width = 720
      winnerBg.height = 244
      this.userGroup.addChild(winnerBg)
      var winner = new eui.Image('pdk-winner_png')
      winner.right = 78
      winner.top = 3
      this.userGroup.addChild(winner)
    }
    if (this.player.owner) {
      var owner = new eui.Image('pdk-owner_png')
      owner.right = 88
      owner.top = 16
      this.userGroup.addChild(owner)
    }
    if (this.player.winner && this.player.owner) {
      winner.right = 112
      owner.right = 36
    }

    const yellowColor = 0xf6d853
    this.userid.textColor = yellowColor
    if (player.point >= 0) {
      this.zhanjiText.source = 'pdk-active-total_png'
      this.highest.textColor = yellowColor
      this.boom.textColor = yellowColor
      this.winlose.textColor = yellowColor
      // this.username.textColor = 0xDC143C
    } else {
      this.zhanjiText.source = 'pdk-default-total_paodekuai_png'      
    }

    var hLayoutScore:eui.HorizontalLayout = new eui.HorizontalLayout()
    hLayoutScore.gap = 0
    hLayoutScore.paddingRight = 30
    hLayoutScore.horizontalAlign = egret.HorizontalAlign.CENTER
    this.userScore.layout = hLayoutScore   /// 水平布局

    const scoreNumber = this.scoreNumber(this.player.point)
    scoreNumber.forEach(number => {
      this.userScore.addChild(new eui.Image(number))
    })

    if (this.$hasAddToStage) {
      window.eventDispatcher.dispatchEvent(new egret.Event('ok'))
    } else {
      this.once(eui.UIEvent.ADDED_TO_STAGE, function () {
        setTimeout(() => {
          window.eventDispatcher.dispatchEvent(new egret.Event('ok'))
        }, window.labelRenderTime)
      }, this)
    }
  }
}

export default UserLabel